var comJs = require('../../../public/js/common_func.js')
const app = getApp();
Page({
  data: {
    hasUserInfo: false,
    userInfo: null,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    sendCode: false,
    sec: 60,
    phone: '',
    phoneValid: false,
    pwdValid: false,
    logo_path: '../../../images/public/app_logo.png',
    close_path: '../../../images/public/close.png',
    vertificationText: '获取验证码',
    rollbackPage: null
  },
  phoneNumInputEvent: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  getCode: function (e) {
    var _this = this;
    if (_this.data.phone.trim() == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    var time = _this.data.sec
    comJs.getCode(_this, time);
  },
  closeReg: function () {
    console.log('close reg');
    wx.hideLoading();
    wx.switchTab({
      url: '../../index/index/index',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      rollbackPage: decodeURIComponent(options.rollback)
    })

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.data.object.weChatUser,
          hasUserInfo: true
        })
      }
    } else {
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(app.globalData.userInfo);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  formSubmit: function (e) {
    var userData = e.detail.value;
    if (comJs.validateMobile(userData.phoneNum) && comJs.validateVerifyCode(userData.verifyCode) && comJs.validatePwds(userData.password, userData.repassword)) {

      var _this = this;
      wx.request({
        method: 'POST',
        url: app.globalData.apiUrl + 'api/register',
        data: {
          openId: app.globalData.openId,
          phone: userData.phoneNum,
          verifyCode: userData.verifyCode,
          password: userData.password,
          userInfo: app.globalData.userInfo,
        },
        header: { 'Content-Type': 'application/json' },
        success: function (res) {
          if (res.data.status == 'ok') {
            console.log(_this.data.rollbackPage)
            wx.showToast({
              title: '注册成功',
              success: function () {
                app.globalData.token = res.data.data.token;
                if (_this.data.rollbackPage.indexOf('?') != -1) {
                  wx.redirectTo({
                    url: _this.data.rollbackPage
                  })
                } else {
                  wx.switchTab({
                    url: _this.data.rollbackPage
                  })
                }

              }
            })
          } else {
            wx.showModal({
              title: '用户注册失败',
              showCancel: false,
              content: '注册失败' + res.data.errorMsg,
              success: function (res) {
              }
            })
          }
        },
        fail: function (e) {

        }
      })
    } else {
      //用户输入验证失败
      console.log('validate fail');
    }
  },
  getUserInfo: function (e) {
    var _this = this;
    if (e.detail.errMsg && e.detail.errMsg === "getUserInfo:fail auth deny") {
      wx.openSetting({
        success: function (res) {
          if (!res.authSetting["scope.userInfo"] || !res.authSetting["scope.userLocation"]) {
            wx.getUserInfo({
              success: res => {
                app.globalData.userInfo = res.userInfo
                _this.setData({
                  userInfo: res.userInfo,
                  hasUserInfo: true
                })
              }
            })
          }
        }
      })
    } else {
      app.globalData.userInfo = e.detail.userInfo
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
    }
  },
  getPhoneNumber: function (e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
  } 
})